<template>
  <div class="classfiy">
    <el-form
      :model="ClassForm"
      ref="ClassForm"
      :rules="rules"
      class="demo-ruleForm"
      label-width="100px"
    >
      <el-form-item label="分类名称" prop="ClassName">
        <el-input type="text" v-model="ClassForm.ClassName"></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="add('ClassForm')">添加分类</el-button>
    <el-tabs type="border-card">
      <el-tab-pane label="分类管理">
        <el-table :data="tableData" border style="width: 402px">
          <el-table-column
            prop="id"
            label="id"
            width="50px"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="name"
            label="分类名称"
            width="150px"
            align="center"
          ></el-table-column>
          <el-table-column label="操作 " width="100px" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="edit(scope.row)">编辑</el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100px" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="del(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
export default {
  name: "Classfiy",
  data() {
    return {
      ClassForm: {
        ClassName: "",
      },
      tableData: [],
      rules: {
        ClassName: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            min: 1,
            max: 11,
            message: "长度在 1 到 11 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.getDate();
  },
  methods: {
    getDate() {
      this.$http.get("/classify").then(
        (res) => {
          //         const arr = [];
          //   // 遍历取出数据
          //   for (let i in res.data) {
          //     arr.push({ id: res.data[i].id, name: res.data[i].name });
          //   }
          //   // 赋值到分类列表当中
          //   this.tableData = arr;
          this.tableData = res.data;
        },
        (err) => {
          console.log(err);
        }
      );
    },
    add(formName) {
      if (this.ClassForm.ClassName == "") {
        this.$message({
          message: "内容不能为空！",
          type: "error",
        });

        return false;
      }
      const data = { name: this.ClassForm.ClassName };

      this.$http.post("classify", data).then(
        (res) => {
          this.$refs[formName].resetFields();
          this.$message({
            message: "添加成功！",
            type: "success",
          });
          this.getDate();
        },
        (err) => {
          console.log(err);
        }
      );
    },
    edit(index) {
      this.$prompt("编辑分类", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then((value) => {
          // 判断输入是否为空
          if (value.value == null) {
            this.$message({
              type: "error",
              message: "请输入内容!",
            });
            return;
          }
          const data = { name: value.value };
          console.log(data);
          this.$http.put(`classify/${index.id}`, data).then(
            (res) => {
              console.log(data);
              this.$message({
                type: "success",
                message: "修改成功",
              });
              this.getDate();
            },
            (err) => {
              console.log(err);
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    del(index) {
      this.$confirm("确认是否删除", "提示", {
        confirmButtonText: "qx",
        confirmButtonText: "确定",
        type: "warning",
      })
        .then(() => {
          this.$http.delete(`classify/${index.id}`).then(
            (res) => {
              this.$message({
                type: "success",
                message: "删除成功",
              });
              this.getDate();
            },
            (err) => {
              console.log(err);
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="less" scoped>
.classfiy {
  .demo-ruleForm {
    width: 400px;
    display: inline-block;
    margin: 0px 10px 0px 0px;
  }
  .el-table {
    margin: 0px 0px 0px 20px;
  }
  // 使用深度选择器
  /deep/ td {
    padding: 0px;
    height: 50px;
    line-height: 50px;
  }
}
</style>